package org.openstreetmap.travelingsalesman.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.openstreetmap.osm.Settings;
import org.openstreetmap.travelingsalesman.gui.MainFrame;
import org.openstreetmap.travelingsalesman.gui.ModifyProfileFrame;

public class DeleteProfile extends AbstractAction {

    /**
     * generated.
     */
    private static final long serialVersionUID = -8136640455463445179L;

    /**
     * Constructor for this action.
     */
    public DeleteProfile() {
        super(MainFrame.RESOURCE.getString("Actions.DeleteProfile.Label"));
        putValue(Action.SHORT_DESCRIPTION, MainFrame.RESOURCE.getString("Actions.DeleteProfile.Description"));
        /*
         * action.putValue(Action.SMALL_ICON, new ImageIcon(TaskPaneMain.class
         * .getResource(iconPath)));
         */
    }

    /**
     * ${@inheritDoc}.
     */
    @Override
    public void actionPerformed(final ActionEvent e) 
    {
        JFileChooser fileChooser = new JFileChooser(new File(Settings.getInstance().get("traveling-salesman.loadFile.lastPath", ".")));
        fileChooser.setAcceptAllFileFilterUsed(true);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.addChoosableFileFilter(new FileFilter() {

            @Override
            public boolean accept(final File file) {
                if (file.isDirectory())
                    return true;
                String fileName = file.getName().toLowerCase();
                return fileName.endsWith(".upx");
            }

            @Override
            public String getDescription() {
                return "UPX Profile File (.upx)";
            }
        });
        
        int result = fileChooser.showOpenDialog(null);
        
        if (result == JFileChooser.APPROVE_OPTION) 
        {
            File fileToBeDeleted = fileChooser.getSelectedFile();
            
            if(fileToBeDeleted.getAbsolutePath().indexOf(".upx")>0)
            {
               fileToBeDeleted.delete();
            }
        }
    }
}
